#ifndef DAVDELETE_H
#define DAVDELETE_H
//

#include "base_modul.h"
#include "../qxml.h"
#include "basedav.h"
#include "networkdav.h"
#include "davpropfind.h"
///////////////#include "davdirrecursive.h"

typedef QMap<int, QStringList> DirLevel;

/*

read reverse a qstring list 
for(int i=xx.size()-1; i>=0;i--){

}

*/



//
class DavDelete : public QObject, public Base_Modul
{
    Q_OBJECT
//
public: 
    DavDelete( NetworkDav * db , QString path )
{
    qDebug() << "##DavDelete##### request to file or dir...  -> " << path;
    db = db;
    loopnummer = -1;
    davuser = db->User();
    davpass = db->Pass();
    davhost = db->Host();
    davport = db->Port();
    actualurl = path;
    startpath = path;
    RemoveItem(path);
}
void  SetPosition( int position )
{
  loopnummer =   position;
}
protected:
    

void RemoveItem( const QString path  )
{   
    RunningPath = path;
    davinfo = new QHttp();
    davinfo->setHost(davhost,davport);
    davinfo->setUser(davuser,davpass);
    david = davinfo->request(BuildHeaderRM(path),0,0);
    //////////////connect(davinfo, SIGNAL(requestFinished(int, bool)), this, SLOT(RegisterItems(int, bool)));
    connect(davinfo, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(RegisterItems(const QHttpResponseHeader &)));
    
    qDebug() << "###david  -> " << david;
    
}

QHttpRequestHeader BuildHeaderRM( const QString path )
{
    QHttpRequestHeader header("DELETE", path ,1,1);   /* header */
    header.setValue("Host", davhost );
    header.setValue("User-Agent", WEBAGENTNAME );
    header.setValue("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    header.setValue("Accept-Language", "it,de-de;q=0.8,it-it;q=0.6,en-us;q=0.4,en;q=0.2");
    header.setValue("Connection", "keep-alive");
    return header;
}



private:
  /* container level from dir */
  DirLevel OnDownLevel;
  int IdLevelX;
  int LevelRunnging;
  QString infodirfile;
  QString startpath;
  QString davuser;
  QString davpass;
  QString davhost;
  quint16 davport;
  QString RunningPath;
  NetworkDav *db;
  QHttp *davinfo;
  QDomDocument doc;
  int david;
  int loopnummer;
  int numerazione;
  QSettings setter;
  QStringList RegiDir;
  QStringList RegiFile;
  QStringList WorkingDirs;
  QFile *BeamUpFile;
  QFile *BeamLogFile;
  QFile *XmlFile;
  QString actualurl;
  QString pathregister;
  QString xmlinfo;
  QString dummypath;
  QString DisplayName;
  int statuscode;
signals:
  void CloseDeleteAction( QString path );
  void Confirmer();
  void ConChain( int nextter  );
  void WarningMsg( QString e );
public slots:
void  FlushAll()
{
   emit CloseDeleteAction(startpath);
}
void  SendWarnig( QString bb )
{
   emit WarningMsg(bb);
}
 void  RegisterItems( const QHttpResponseHeader &responseHeader )
{
    davinfo->close();
    int statusbecome = responseHeader.statusCode(); 
    
    qDebug() << "###statusCode  -> " << statusbecome;
    
    if (statusbecome != 204) {
       emit WarningMsg(tr("Error on url: %1\nErrorMsg %2\nHeader Status code %3").arg(actualurl).arg(davinfo->errorString()).arg(statusbecome)); 
    return;        
    } else {
        if (loopnummer == 0 or loopnummer > 0) {
               int next = loopnummer + 1;
           emit ConChain(next);
        } else {
           emit Confirmer();  
        }
    }
    
    
    
    
    
}
};
//
#endif // DAVDELETE_H



